package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbZyzjiuzhuMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjgzZyzjiuzhuInfoMainService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbZyzjiuzhuResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 帮教志愿者
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzZyzjiuzhuInfoMainServiceImpl extends ServiceImpl<AbZyzjiuzhuMapper,AbZyzjiuzhuResultVo> implements BjgzZyzjiuzhuInfoMainService {

    @Resource
    private AbZyzjiuzhuMapper abZyzjiuzhuMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;

    /**
     * 保存帮教--志愿者
     * @param dataStr dataStr
     * @return Result
     */
    @Override
    public Result saveBjZyzInfo(String dataStr,UserDTO user) {
        Date date = new Date();
        AbZyzjiuzhuResultVo abZyzjiuzhuResultVo = JsonUtil.toBean(dataStr,AbZyzjiuzhuResultVo.class);
        //新增
        if(StringUtils.isBlank(abZyzjiuzhuResultVo.getId())) {
            abZyzjiuzhuResultVo.setId(IdGen.strid());
            abZyzjiuzhuResultVo.setAdduserid(user.getDepartid());
            abZyzjiuzhuResultVo.setCreateUser(user.getUsername());
            abZyzjiuzhuResultVo.setCreateTime(date);
        }
        abZyzjiuzhuResultVo.setUpdateUser(user.getUsername());
        abZyzjiuzhuResultVo.setUpdateTime(date);
        boolean tag = abZyzjiuzhuResultVo.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取帮教--志愿者--信息
     * @param pp pp
     * @return Result
     */
    @Override
    public Result getBjZyzBaseInfo(String pp) {
        Map<String,Object> map = abZyzjiuzhuMapper.getBjZyzBaseInfo(pp);
        List<Map<String,Object>> appendixBjZyzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,"18",null);
        if(MapUtils.isNotEmpty(map)) {
            map.put("appendixBjZyzList",appendixBjZyzList);
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}